package cn.devezhao.bizz.privileges.impl;

import cn.devezhao.bizz.privileges.Permission;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
/**
 * @author WangRuiJie
 * @Title: BizzPermission
 * @Package cn.devezhao.bizz.privileges.impl
 * @Description:
 * @date 2022/1/204:56 下午
 */

public class BizzPermission implements Permission {
    private static final long serialVersionUID = -3736314513059920238L;
    public static final Permission CREATE = new BizzPermission("C", 1, false);
    public static final Permission DELETE = new BizzPermission("D", 2, true);
    public static final Permission UPDATE = new BizzPermission("U", 4, true);
    public static final Permission READ = new BizzPermission("R", 8, true);
    public static final Permission ASSIGN = new BizzPermission("A", 16, true);
    public static final Permission SHARE = new BizzPermission("S", 32, true);
    public static final Permission NONE = new BizzPermission("N", 0, false);
    public static final Permission PUT_1 = new BizzPermission("P", 64, true);
    public static final Permission RECYCLE = new BizzPermission("Y", 128, true);
    public static final Permission GET_1 = new BizzPermission("G", 256, true);
    public static final Permission DELETE_1 = new BizzPermission("D1", 512, true);
    public static final Permission ASSIGN_1 = new BizzPermission("A1", 1024, true);
    public static final Permission LOCK = new BizzPermission("L", 2048, true);
    private final int mask;
    private final String name;
    private final boolean needGuard;

    public static Permission[] parse(int masks) {
        List<Permission> bps = new ArrayList(3);
        if ((masks & CREATE.getMask()) != 0) {
            bps.add(CREATE);
        }

        if ((masks & DELETE.getMask()) != 0) {
            bps.add(DELETE);
        }

        if ((masks & UPDATE.getMask()) != 0) {
            bps.add(UPDATE);
        }

        if ((masks & READ.getMask()) != 0) {
            bps.add(READ);
        }

        if ((masks & ASSIGN.getMask()) != 0) {
            bps.add(ASSIGN);
        }

        if ((masks & SHARE.getMask()) != 0) {
            bps.add(SHARE);
        }

        if ((masks & PUT_1.getMask()) != 0) {
            bps.add(PUT_1);
        }

        if ((masks & RECYCLE.getMask()) != 0) {
            bps.add(RECYCLE);
        }

        if ((masks & GET_1.getMask()) != 0) {
            bps.add(GET_1);
        }

        if ((masks & DELETE_1.getMask()) != 0) {
            bps.add(DELETE_1);
        }

        if ((masks & ASSIGN_1.getMask()) != 0) {
            bps.add(ASSIGN_1);
        }

        if ((masks & LOCK.getMask()) != 0) {
            bps.add(LOCK);
        }

        return (Permission[])bps.toArray(new Permission[0]);
    }

    public BizzPermission(String name, int mask) {
        this(name, mask, Boolean.FALSE);
    }

    public BizzPermission(String name, int mask, boolean needGuard) {
        this.name = name;
        this.mask = mask;
        this.needGuard = needGuard;
    }

    public Serializable getIdentity() {
        return this.getName() + ':' + this.getMask() + ':' + (this.isNeedGuard() ? 1 : 0);
    }

    public int getMask() {
        return this.mask;
    }

    public String getName() {
        return this.name;
    }

    public boolean isNeedGuard() {
        return this.needGuard;
    }

    public String toString() {
        return this.getIdentity() + ":BizzPermission@" + super.hashCode();
    }
}

